package com.bookkeeping.cn.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bookkeeping.cn.LoginActivity;
import com.bookkeeping.cn.databinding.FragmentMineBinding;
import com.bookkeeping.cn.util.OkHttpUtil;
import com.bookkeeping.cn.util.URLUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class MineFragment extends Fragment {

    private FragmentMineBinding binding;
    TextView usernameTv;
    TextView realnameTv;
    TextView countTv;
    Button outBt;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMineBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        usernameTv = binding.usernameTv;
        realnameTv = binding.realnameTv;
        countTv = binding.countTv;
        outBt = binding.outBt;

        // 设置退出按钮点击事件
        outBt.setOnClickListener(v -> logout());

        getDetail();
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void getDetail() {
        Request request = new Request.Builder()
                .url(URLUtil.GET_USER_INFO)
                .get()
                .addHeader("Authorization",  URLUtil.TOKEN)
                .build();

        OkHttpUtil.getInstance().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                showToast("网络请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                JSONObject jsonObject = JSON.parseObject(str);

                if (jsonObject.getInteger("code") == 1) {
                    getActivity().runOnUiThread(() -> {
                        usernameTv.setText(jsonObject.getJSONObject("content").getString("username"));
                        realnameTv.setText(jsonObject.getJSONObject("content").getString("realname"));
                        countTv.setText(jsonObject.getJSONObject("content").getInteger("count") + "");
                    });
                }
                if(jsonObject.getInteger("code")==-9){
                    logout();
                }
                showToast(jsonObject.getString("message"));
            }
        });
    }

    private void logout() {
        SharedPreferences.Editor editor=getActivity().getSharedPreferences("token_file", Context.MODE_PRIVATE).edit();
        editor.remove("token");
        editor.apply();
        startActivity(new Intent(getContext(),LoginActivity.class));
        getActivity().finish();
    }

    private void showToast(String message) {
        getActivity().runOnUiThread(() ->
                Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show()
        );
    }

}